<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>盒子居中-未知盒子大小</title>
  <style>
    /* 未知盒子大小的居中
    position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);

    1. 子绝父相：position: absolute;
    2. 子盒子左上角放在父盒子中心：left: 50%;top: 50%;
    3. 子盒子向左偏移50%，子盒子向上偏移50%：transform: translate(-50%, -50%);
    */

    .father {
      position: relative;
      width: 800px;
      height: 800px;
      background-color: pink;
    }

    .son {
      width: 10%;
      height: 20%;
      background-color: purple;
      /* 核心代码 */
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="son"></div>
  </div>
</body>
</html>